import 'dart:async';

import 'package:BeeApp/MainPage/MainToolPage.dart';
import 'package:BeeApp/MainPage/http_request.dart';
import 'package:flutter/material.dart';

class ChangePWPage extends StatefulWidget {
  ChangePWPage({Key key}) : super(key: key);

  @override
  _ChangePWPageState createState() => _ChangePWPageState();
}

class _ChangePWPageState extends State<ChangePWPage> {
  String yzmText = "发送验证码";
  Timer countDownTimer;

  final mobileText = TextEditingController();
  final codeText = TextEditingController();
  final passwordText = TextEditingController();
  final repasswordText = TextEditingController();

  @override
  void dispose() {
    super.dispose();
    countDownTimer?.cancel();
  }

  TextStyle textStyle() {
    return TextStyle(
      fontSize: setFontSize(32),
      color: Color(0xFF333333),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: getAppBar(context, "忘记密码"),
        backgroundColor: Colors.white,
        body: GestureDetector(
          behavior: HitTestBehavior.translucent,
          onTap: () {
            FocusScope.of(context).requestFocus(FocusNode());
          },
          child: ListView(
            children: [
              SizedBox(height: 20),
              mobileVirw(),
              lineView(),
              codeVirw(),
              lineView(),
              passWordVirw01(),
              lineView(),
              passWordVirw02(),
              lineView(),
              SizedBox(height: 40),
              Container(
                padding: EdgeInsets.all(20),
                child: addView(),
              )
            ],
          ),
        ));
  }

  Widget lineView() {
    return Container(height: 1, color: Color(0xFFDCDCDC));
  }

  Widget mobileVirw() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 20, right: 20),
      child: Row(
        children: [
          Expanded(
              child: TextField(
            keyboardType: TextInputType.number,
            controller: mobileText,
            textAlign: TextAlign.start,
            cursorColor: Colors.grey[400], //设置光标
            decoration: InputDecoration(
                border: InputBorder.none,
                hintText: "请输入手机号",
                hintStyle: TextStyle(
                    fontSize: setFontSize(30), color: Color(0xFFAEAEAE))),
            style: textStyle(),
          )),
          OutlineButton(
            onPressed: yzmText == "发送验证码" ? getCode : null,
            padding: EdgeInsets.only(top: 0),
            borderSide: BorderSide(color: maincolor),
            highlightedBorderColor: maincolor,
            child: Text(yzmText),
            textColor: maincolor,
          ),
        ],
      ),
    );
  }

  void getCode() {
    if (!isPhone(mobileText.text)) {
      showToast("请输入正确手机号");
      return;
    }
    DioUtils.postHttp(
      context,
      "user/send_code",
      parameters: {
        "user_type": getData("AppType"),
        "mobile": mobileText.text,
        "event": "resetpwd"
      },
      onSuccess: (t) {
        print(t["msg"]);
        if (t["code"].toString() == "1") {
          // ignore: unnecessary_statements
          yzmText == "发送验证码" ? codeGet() : null;
        } else {
          showToast(t["msg"]);
        }
      },
      onError: (error) {
        print("$error");
      },
    );
  }

  codeGet() {
    countDownTimer?.cancel(); //如果已存在先取消置空
    countDownTimer = null;
    countDownTimer = new Timer.periodic(new Duration(seconds: 1), (t) {
      if (mounted) {
        setState(() {
          if (60 - t.tick > 0) {
            //60-t.tick代表剩余秒数，如果大于0，设置yzmText为剩余秒数，否则重置yzmText，关闭countTimer
            yzmText = "${60 - t.tick}秒";
          } else {
            yzmText = '发送验证码';
            countDownTimer.cancel();
            countDownTimer = null;
          }
        });
      }
    });
  }

  Widget codeVirw() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 20, right: 20),
      child: TextField(
        keyboardType: TextInputType.number,
        controller: codeText,
        textAlign: TextAlign.start,
        cursorColor: Colors.grey[400], //设置光标
        decoration: InputDecoration(
            border: InputBorder.none,
            hintText: "请输入短信验证码",
            hintStyle:
                TextStyle(fontSize: setFontSize(30), color: Color(0xFFAEAEAE))),
        style: textStyle(),
      ),
    );
  }

  Widget passWordVirw01() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 20, right: 20),
      child: TextField(
        obscureText: true,
        controller: passwordText,
        textAlign: TextAlign.start,
        cursorColor: Colors.grey[400], //设置光标
        decoration: InputDecoration(
            border: InputBorder.none,
            hintText: "请输入新密码",
            hintStyle:
                TextStyle(fontSize: setFontSize(30), color: Color(0xFFAEAEAE))),
        style: textStyle(),
      ),
    );
  }

  Widget passWordVirw02() {
    return Container(
      color: Colors.white,
      padding: EdgeInsets.only(left: 20, right: 20),
      child: TextField(
        obscureText: true,
        controller: repasswordText,
        textAlign: TextAlign.start,
        cursorColor: Colors.grey[400], //设置光标
        decoration: InputDecoration(
            border: InputBorder.none,
            hintText: "再次输入密码",
            hintStyle:
                TextStyle(fontSize: setFontSize(30), color: Color(0xFFAEAEAE))),
        style: textStyle(),
      ),
    );
  }

  Widget addView() {
    return Container(
      child: InkWell(
        child: Container(
          alignment: Alignment.center,
          width: 900,
          height: setHeight(100),
          decoration: BoxDecoration(
            color: maincolor,
            borderRadius: BorderRadius.all(
              Radius.circular(setHeight(10)),
            ),
          ),
          child: Text(
            "重置",
            style: TextStyle(color: Colors.white, fontSize: setFontSize(34)),
          ),
        ),
        onTap: () {
          // ...
          if (!isPhone(mobileText.text)) {
            showToast("请输入正确的手机号");
            return;
          }
          if (codeText.text.length == 0) {
            showToast("请输入验证码");
            return;
          }
          if (passwordText.text.length < 6) {
            showToast("请输入6位以上的密码");
            return;
          }
          if (passwordText.text != repasswordText.text) {
            showToast("请确认密码一致");
            return;
          }
          showLoading(context);
          DioUtils.postHttp(
            context,
            "user/resetpwd",
            parameters: {
              "user_type": getData("AppType"),
              "mobile": mobileText.text,
              "code": codeText.text,
              "newpwd": passwordText.text,
              "repwd": repasswordText.text
            },
            onSuccess: (t) {
              print(t);
              dismissLoading(context);
              showToast(t["msg"].toString());
              Map dic = t;
              if (dic["code"] == 1) {
                setData("token", "");
                Navigator.pop(context);
              }
            },
            onError: (error) {
              dismissLoading(context);
              print(error);
            },
          );
        },
      ),
    );
  }
}
